package com.three.Controller;

import com.three.Pojo.User;
import com.three.Service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    //登陆页面
    @RequestMapping("/toLogin.do")
    public String toLogin(){
        return "LoginOREnroll";
    }

    @RequestMapping("/index.do")
    public String toindex(){
        return "template";
    }

    //验证登录信息，成功登录/失败登录
    @RequestMapping("/login.do")
    public String login(String username, String password, ModelMap map, HttpSession session) {
        User user = userService.selUser(username, password);
        if (user == null) {
            map.addAttribute("massage","用户名或密码错误!!!");
            return "LoginOREnroll";
        } else {
            session.setAttribute("user",user);
            return "template";
        }
    }

    //提交注册信息保存到user
    @RequestMapping("/enroll.do")
    public String enroll(User user){
        userService.AddUser(user);
        return "redirect:/user/toLogin.do";
    }

    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "redirect:/user/toLogin.do";
    }

}

